import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/home.vue'
import store from "@/store";
import {notification} from "ant-design-vue";

const routes = [
  {
    path: '/',
    name: 'home',
    component: HomeView,
    meta: {
      title: "12306购票系统后台",
      loginRequire: true
    },
    children: [{
      path: 'welcome',
      component: () => import('../views/main/welcome.vue'),
    },
      {
        path: 'base/',
        children:[{
          path: 'station',
          component: () => import('../views/base/station.vue'),
          meta: {
            title: "车站管理"
          }
        },
          {
            path: 'train',
            component: () => import('../views/base/train.vue'),
            meta:{
              title: "车次管理"
            }
          },
          {
            path: 'train-station',
            component: () => import('../views/base/train-station.vue'),
            meta: {
              title: "车次车站"
            }
          },
          {
            path: 'train-carriage',
            component: () => import('../views/base/train-carriage.vue'),
            meta: {
              title: "车站车厢管理"
            }
          },
          {
            path: 'train-seat',
            component: () => import('../views/base/train-seat.vue'),
            meta:{
              title: "车次座位管理"
            }
          }]
      }, {
        path: 'business/',
        children: [ {
          path: 'daily-train',
          component: () => import('../views/business/daily-train.vue'),
          meta: {
            title: "每日车次管理"
          }
        }, {
          path: 'daily-train-carriage',
          component: () => import('../views/business/daily-train-carriage.vue'),
          meta: {
            title: "每日车次车厢管理"
          }
        }, {
          path: 'daily-train-seat',
          component: () => import('../views/business/daily-train-seat.vue'),
          meta: {
            title: "每日车次车座管理"
          }
        }, {
          path: 'daily-train-station',
          component: () => import('../views/business/daily-train-station.vue'),
          meta: {
            title: "每日车次车站管理"
          }
        }, {
          path: 'daily-train-ticket',
          component: () => import('../views/business/daily-train-ticket.vue'),
          meta: {
            title: "每日车次车票管理"
          }
        }, {
          path: 'confirm-order',
          component: () => import('../views/business/confirm-order.vue'),
          meta: {
            title: "订单管理"
          }
        }, {
          path: 'sk-token',
          component: () => import('../views/business/sk-token.vue'),
          meta: {
            title: "令牌管理"
          }
        }]
      },{
        path: 'batch/',
        children: [
          {
            path: 'job',
            component: () => import('../views/batch/job.vue'),
            meta: {
              title: "定时任务管理"
            }
          }
        ]
      },{
        path: 'member/',
        children: [
          {
            path: 'ticket',
            component: () => import('../views/member/ticket.vue'),
            meta: {
              title: "会员车票管理"
            }
          }
        ]
      }]
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/login.vue'),
    meta:{
      title: "登录-12306购票后台系统"
    }
  },
  {
    path: '',
    redirect: "/welcome"
  }
];

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

// 路由登录拦截
router.beforeEach((to, from, next) => {
  // 要不要对meta.loginRequire属性做监控拦截
  if (to.matched.some(function (item) {
    console.log(item, "是否需要登录校验：", item.meta.loginRequire || false);
    return item.meta.loginRequire
  })) {
    const _admin = store.state.admin;
    console.log("页面登录校验开始：", _admin);
    if (!_admin.token) {
      console.log("用户未登录或登录超时！");
      notification.error({ description: "未登录或登录超时" });
      next('/login');
    } else {
      next();
    }
  } else {
    next();
  }
});

export default router
